<?php
require_once ROOT_APPLICATION_MODULES_ADMIN_FORMS.DS."Article.php";
require_once ROOT_APPLICATION_MODULES_ADMIN_OBJECTS.DS."Article.php";
require_once ROOT_APPLICATION_MODULES_ADMIN_MODELS.DS."Articles.php";

require_once ROOT_APPLICATION_MODULES_ADMIN_OBJECTS.DS."TagItemAssoc.php";
require_once ROOT_APPLICATION_MODULES_ADMIN_MODELS.DS."TagItemAssocs.php";

require_once '../library/Utility/Converter.php';

class ArticleController extends Zend_Controller_Action {

	public function init() {
		
	}
	
	public function indexAction() {
  		$articleModel = new Application_Model_Articles();
  		$articles = $articleModel->fetchAll();
  		$this->view->articles = $articles;
	}
	
	public function addAction() {
		$form = new Application_Form_Article();
        $form->submit->setLabel('Save');
        $this->view->form = $form;
       
        if ($this->getRequest()->isPost()) 
        {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) 
            {
                /* Input value */
            	            	
                $article_id = $form->getValue('article_id');
				$category_id = $form->getValue('category_id');
				$title = $form->getValue('title');
				$sub_title = $form->getValue('sub_title');
				$slug = '';
				$description = $form->getValue('description');
				$content = $form->getValue('content');
				$author = $form->getValue('author');
				$icons = '';
				$image_square = '';
				$image_thumbnail = '';
				$image_small = '';
				$image_crop = '';
				$image_medium = '';
				$image_large = '';
				$status = 'inactive';
				$num_views = 0;
				$created_date = Utility_Converter::getCurrentTime();
				$created_user_id = $form->getValue('created_user_id');
				if($created_user_id == "")
					$created_user_id = null;
				// Need user module.
				$created_user_name = $form->getValue('created_user_name');
				$updated_date = null;
				$updated_user_id = null;
				$updated_user_id = null;
				$updated_user_name = null;
				$activate_user_id = null;
				$activate_user_name = null;
				$activate_date = null;
				$allow_comment = $form->getValue('allow_comment');
				$num_comments = 0;
				$is_hot = $form->getValue('is_hot');
				$show_date = null;
				$sticky = $form->getValue('sticky');
				$language = $form->getValue('language');
                
                /* Create new article model */
                
				$article = new Article(
						$article_id,
						$category_id,
						$title,
						$sub_title,
						$slug,
						$description,
						$content,
						$author,
						$icons,
						$image_square,
						$image_thumbnail,
						$image_small,
						$image_crop,
						$image_medium,
						$image_large,
						$status,
						$num_views,
						$created_date,
						$created_user_id,
						$created_user_name,
						$updated_date,
						$updated_user_id,
						$updated_user_name,
						$activate_user_id,
						$activate_user_name,
						$activate_date,
						$allow_comment,
						$num_comments,
						$is_hot,
						$show_date,
						$sticky,
						$language);						
                $article_model = new Application_Model_Articles();
                $inserted_id = $article_model->_insert($article);	
                
                /* Add tag assoc */
                
                $tags = $form->getValue('tag');
                $this->InsertTags($tags, $inserted_id);
                
                // Redirect after insert.
                $param = array ("article_id"=>$inserted_id);
                $this->_helper->redirector("edit", "article", "admin", $param);					
            } else 
            {
                $form->populate($formData);
            }
        }
	}
	
	public function editAction() {
    	$form = new Application_Form_Article();
        $form->submit->setLabel('Save');
        $this->view->form = $form;
       
        if ($this->getRequest()->isPost()) 
        {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) 
            {
                /* Input value */
            	            	
                $article_id = $form->getValue('article_id');
				$category_id = $form->getValue('category_id');
				$title = $form->getValue('title');
				$sub_title = $form->getValue('sub_title');
				$slug = '';
				$description = $form->getValue('description');
				$content = $form->getValue('content');
				$author = $form->getValue('author');
				$icons = '';
				$image_square = '';
				$image_thumbnail = '';
				$image_small = '';
				$image_crop = '';
				$image_medium = '';
				$image_large = '';
				$status = $form->getValue('status');
				$num_views = $form->getValue('num_views');
				$created_date = $form->getValue('created_date');
				$created_user_id = $form->getValue('created_user_id');
				if($created_user_id == "")
					$created_user_id = null;
				$created_user_name = $form->getValue('created_user_name');
				$updated_date = Utility_Converter::getCurrentTime();
				$updated_user_id = $form->getValue('updated_user_id');
				if($updated_user_id == "")
					$updated_user_id = null;
				$updated_user_name = $form->getValue('updated_user_name');
				$activate_user_id = $form->getValue('activate_user_id');
				if($activate_user_id == "")
					$activate_user_id = null;
				$activate_user_name = $form->getValue('activate_user_name');
				$activate_date = $form->getValue('activate_date');
				if($activate_date == "")
					$activate_date = null;
				$allow_comment = $form->getValue('allow_comment');
				$num_comments = $form->getValue('num_comments');
				$is_hot = $form->getValue('is_hot');
				$show_date = $form->getValue('show_date');
				if($show_date == "")
					$show_date = null;
				$sticky = $form->getValue('sticky');
				$language = $form->getValue('language');
                
                /* Create new article model */	
                
				$article = new Article(
						$article_id,
						$category_id,
						$title,
						$sub_title,
						$slug,
						$description,
						$content,
						$author,
						$icons,
						$image_square,
						$image_thumbnail,
						$image_small,
						$image_crop,
						$image_medium,
						$image_large,
						$status,
						$num_views,
						$created_date,
						$created_user_id,
						$created_user_name,
						$updated_date,
						$updated_user_id,
						$updated_user_name,
						$activate_user_id,
						$activate_user_name,
						$activate_date,
						$allow_comment,
						$num_comments,
						$is_hot,
						$show_date,
						$sticky,
						$language);						
                $article_model = new Application_Model_Articles();
                $article_model->_update($article);		

                /* Update assoc tag */
                
                $tags = $form->getValue('tag');
                //
                // Delete assoc
				$tag_item_assoc_model = new Application_Model_Tag_Item_Assocs();
				$tag_item_assoc_model->_delete($article_id);
				//
				// Insert assoc tag
				$this->InsertTags($tags, $article_id);
                
                
            } else 
            {
                $form->populate($formData);
            }
        } else 
        {
            $article_id = $this->_getParam('article_id');
            if ($article_id > 0) 
            {
                $article_model = new Application_Model_Articles();
                $articleArray = Utility_Converter::objectToArray($article_model->_find($article_id));
                $form->populate($articleArray);
            }
        }
	}
	
	public function deleteAction() {
		$article_id = $this->_getParam('article_id');
		
		/* Delete assoc */
		
		$tag_item_assoc_model = new Application_Model_Tag_Item_Assocs();
		$tag_item_assoc_model->_delete($article_id);
		
		/* Delete article */
		
		$article_model = new Application_Model_Articles();
		$article_model->_delete($article_id);
		
		
		$this->_helper->redirector('index');
	}
	
	/**
	 * 
	 * Insert tags to database
	 * @param unknown_type $tag
	 */
	private function InsertTags($tag, $article_id) {
		foreach ($tag as $t) {
		// Create tag_item_assoc model
		$tag_item_assoc_model = new Application_Model_Tag_Item_Assocs();
		
		// Create tag_item_assoc member value
		
		$tag_id = $t;
		$item_id = $article_id;
		$item_name = "article_id";
		$route_name = "news_article_details";
		$details_route_name = "news_tag_article";
		$params = "article_id:" . $item_id; // ex: article_id:1
		
		// Create new object
		$tag_item_assoc_object = new Tag_Item_Assoc(					  	
			$tag_id,
			$item_id,
			$item_name,
			$route_name,
			$details_route_name,
			$params);
		// Insert to database
		$tag_item_assoc_model->_insert($tag_item_assoc_object);
		}
	}
	
}